<template>
  <div id="app">
    <nav class="navbar">
      <div class="logo">校园活动管理系统</div>
      <ul class="nav-links">
        <li><router-link to="/">首页</router-link></li>
        <li><router-link to="/public-activities">公开活动</router-link></li>
        <li v-if="isAdmin"><router-link to="/activity-management">活动管理</router-link></li>
        <li v-if="isLoggedIn">
          <span>欢迎，{{ userRole === 'admin' ? '管理员' : '同学' }}</span>
        </li>
        <li v-if="isLoggedIn">
          <a href="#" @click.prevent="logout">退出登录</a>
        </li>
      </ul>
    </nav>
    
    <div class="container">
      <router-view/>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoggedIn: false,
      isAdmin: false,
      userRole: null
    }
  },
  mounted() {
    this.checkAuthStatus()
  },
  watch: {
    '$route': 'checkAuthStatus'
  },
  methods: {
    checkAuthStatus() {
      this.userRole = localStorage.getItem('userRole')
      this.isLoggedIn = !!this.userRole
      this.isAdmin = this.userRole === 'admin'
    },
    logout() {
      localStorage.removeItem('userRole')
      this.checkAuthStatus()
      this.$router.push('/login')
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

.navbar {
  background-color: #2c3e50;
  color: white;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 1000;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
}

.nav-links {
  list-style: none;
  display: flex;
  margin: 0;
  padding: 0;
  gap: 1.5rem;
}

.nav-links li {
  margin: 0;
}

.nav-links a {
  color: white;
  text-decoration: none;
  transition: opacity 0.3s;
}

.nav-links a:hover {
  opacity: 0.8;
}

.container {
  margin-top: 70px; /* 留出导航栏的空间 */
  padding: 2rem;
}

/* 修改一些全局样式以确保页面内容正确显示 */
body {
  margin: 0;
  padding: 0;
}
</style>